import instacne from "../request";
import type { Ref } from "vue";
import qs from "qs";

// 获取页面所有的数据 /flash/list
interface FlashList {
  keyword: string;
  pageNum: number;
  pageSize: number;
}

interface ResData {
  startDate: string;
  endDate: string;
  title: string;
  status: number;
  id: number;
  createTime: null | string;
}

interface FlashListData {
  list: ResData[];
  pageNum: number;
  pageSize: number;
  total: number;
  totalPage: number;
}

const flashListApi = (data: FlashList): Promise<FlashListData> => {
  return instacne.get(
    `/flash/list?pageNum=${data.pageNum}&pageSize=${data.pageSize}&keyword=${data.keyword}`
  );
};

// 修改上下线 /flash/update/status/
interface FlashUpdateStatus {
  id: number;
  status: number;
}
const FlashUpdateStatusApi = (data: FlashUpdateStatus) => {
  return instacne.post(`/flash/update/status/${data.id}?status=${data.status}`);
};

// 删除活动 /flash/delete/
interface FlashDelete {
  id: number;
}
const FlashDeleteApi = (data: FlashDelete) => {
  return instacne.post(`/flash/delete/${data.id}`);
};

// 秒杀时间段 /flashSession/list
interface FlashSessionList {
  data: any[];
  startDate: string;
  endDate: string;
  name: string;
  status: number;
  id: number;
  createTime: null | string;
}

const FlashSessionListApi = (): Promise<FlashSessionList> => {
  return instacne.get("/flashSession/list");
};

// 删除秒杀时间段 /flashSession/delete/
const FlashSessionDelete = (data: number) => {
  return instacne.post(`/flashSession/delete/${data}`);
};

// 添加秒杀时间段 /flashSession/create
interface FlashSessionCreate {
  createTime?: null;
  id?: number;
  endDate?: string;
  title?: string;
  startDate?: string;
  status?: number;
  name?: string;
  startTime?: string;
  endTime?: string;
}

let FlashSessionCreateApi = (data: FlashSessionCreate) => {
  return instacne.post("/flashSession/create", data);
};

// 修改启用状态 /flashSession/update/status/
interface FlashSessionUpdateStatus {
  id: number;
  status: number;
}
const FlashSessionUpdateStatusApi = (data: FlashSessionUpdateStatus) => {
  return instacne.post(
    `/flashSession/update/status/${data.id}?status=${data.status}`
  );
};

// 编辑页面 /flash/update/
const FlashUpdateApi = (data: FlashSessionCreate) => {
  return instacne.post(`/flash/update/${data.id}`, data);
};

// 添加活动 /flash/create
const FlashCreate = (data: FlashSessionCreate) => {
  return instacne.post("/flash/create", data);
};

// 设置商品 /flashSession/selectList
interface FlashSessionSelectList {
  flashPromotionId: string | null;
}

const FlashSessionSelectListApi = (data: FlashSessionSelectList) => {
  return instacne.get(
    `/flashSession/selectList?flashPromotionId=${data.flashPromotionId}`
  );
};

export {
  flashListApi,
  FlashUpdateStatusApi,
  FlashDeleteApi,
  FlashSessionListApi,
  FlashSessionDelete,
  FlashSessionCreateApi,
  FlashSessionUpdateStatusApi,
  FlashUpdateApi,
  FlashCreate,
  FlashSessionSelectListApi,
};
